home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 464 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  111 lines

  1. Path: solon.com!not-for-mail
  2. From: Michael Smith <msmith@mpx.com.au>
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: Leading and Trailing Blanks
  5. Date: 5 Jan 1996 09:53:01 -0600
  6. Organization: Emmenjay Consulting
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4cjhgt$f87@solutions.solon.com>
  10. References: <4chh1b$685@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Mailer: Mozilla 2.0b3 (Win16; I)
  13.  
  14.  
  15. Casey Claiborne wrote:
  16. > Hello -
  17. >         I am wondering if anyone out there has a program (or knows of one)
  18. > that allows one to strip leading and trailing blanks from a string.
  19. > ex:
  20. >         char test[20];
  21. >         strcpy(test,"  TESTING  ");
  22. >         printf("%s", test);
  23. > will produce an output like
  24. >   TESTING
  25. > that has blanks at the beginning of "TESTING". I would like to
  26. > have the following result
  27. > TESTING
  28.  
  29. The following code has not been tested, so probably contains typos.
  30. It should serve to illustrate the algorithm.
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #define TRUE 1
  37. #define FALSE 0
  38.  
  39. char *dupstr( str );    /* defined below */
  40.  
  41. /**************************************************************/
  42. /* trimstr - Remove leading and trailing blanks from a string */
  43. /*           Return TRUE (OK) or FALSE (Error)                */
  44. /**************************************************************/
  45. int trimstr( char *str )
  46. {
  47. {
  48.     char *buff, *start, *end;
  49.     int *len;
  50.  
  51.  
  52.     /* MAKE A COPY OF THE STRING */
  53.     buff = dupstr( str );
  54.     if (buff==NULL) return( FALSE );
  55.  
  56.     /* FIND FIRST NON-BLANK CHAR */
  57.     start = buff;
  58.     while (start==' ') start++;
  59.  
  60.     /* TRIM TRAILING BLANKS */
  61.     len = strlen( start );
  62.     end = start+len-1;
  63.     while (end>=start && *end==' ') *(end--)='\0';
  64.     
  65.     
  66.     /* COPY BACK TO ORIGINAL STRING */
  67.     strcpy( str, start );
  68.  
  69.  
  70.     /* FREE TEMP BUFFER */
  71.     free( buff );
  72.  
  73.     return( TRUE );
  74. }
  75. }
  76.  
  77.  
  78. /*******************************************************************/
  79. /* dupstr - Emulate non-ANSI strdup() function: duplicate a string */
  80. /*******************************************************************/
  81. char *dupstr( char *str )
  82. {
  83. {
  84.     char *newstr = malloc( strlen(str)+1 );
  85.  
  86.  
  87.     if (newstr) strcpy( newstr, str );
  88.     return( newstr );
  89. }
  90. }
  91.  
  92.  
  93. -- 
  94. #####################################################################
  95. Michael Smith                                       msmith@mpx.com.au
  96.                       Emmenjay Consulting 
  97.     Sofware Development          System Administration/Management
  98.     Applications Support              PC Installation/Maintenance
  99.     Training                                    Technical Writing
  100.     Windows/Unix               General Consulting/Troubleshooting
  101.  
  102. PO Box 909                                             Ph 018 240 704
  103. Kensington 2033
  104. AUSTRALIA    
  105. #####################################################################
  106.